# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def minDepth(self, root) -> int:
        depth, short = 0, float('inf')
        if not root: return 0

        def dfs(root):
            nonlocal depth
            nonlocal short
            if not root: return
            depth += 1
            dfs(root.left)
            dfs(root.right)
            if not root.left and not root.right:
                if depth < short:
                    short = depth
            depth -= 1
        dfs(root)
        return short